﻿using IterationLife.Datas;
using IterationLife.PlatformBackEnd.AppServices.Contracts.Systems;
using IterationLife.PlatformBackEnd.AppServices.Dtos.Systems;
using IterationLife.System.Models;
using IterationLife.System.Queries;
using IterationLife.System.Repositories;
using Util.Applications;
using Util.Datas.Queries;
using Util.Domains.Trees;

namespace IterationLife.PlatformBackEnd.AppServices.Impl.Systems
{
    /// <summary>
    /// 行政区域服务
    /// </summary>
    public class DistrictService : TreeServiceBase<District, DistrictDto, DistrictQuery>, IDistrictService
    {
        /// <summary>
        /// 初始化行政区域服务
        /// </summary>
        /// <param name="unitOfWork">工作单元</param>
        /// <param name="districtRepository">行政区域仓储</param>
        public DistrictService(IApplicationUnitOfWork unitOfWork, IDistrictRepository districtRepository)
            : base(unitOfWork, districtRepository)
        {
            DistrictRepository = districtRepository;
        }

        /// <summary>
        /// 行政区域仓储
        /// </summary>
        protected IDistrictRepository DistrictRepository { get; set; }

        /// <summary>
        /// 转换为数据传输对象
        /// </summary>
        /// <param name="entity">实体</param>
        protected override DistrictDto ToDto(District entity)
        {
            return entity.ToDto();
        }

        /// <summary>
        /// 转换为实体
        /// </summary>
        /// <param name="dto">数据传输对象</param>
        protected override District ToEntity(DistrictDto dto)
        {
            return dto.ToEntity();
        }

        /// <summary>
        /// 创建查询对象
        /// </summary>
        /// <param name="param">行政区域查询实体</param>
        protected override IQueryBase<District> CreateQuery(DistrictQuery param)
        {
            return new Query<District>(param)
                .Filter(new TreeCriteria<District>(param))
                .Filter(t => t.Name.Contains(param.Name))
                .Filter(t => t.PinYin.Contains(param.Name))
                .Filter(t => t.FullPinYin.Contains(param.Name));
        }
    }
}